<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Edit_reading extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}

	public function index(){

	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  				'accounts'		=> $this->edit_reading_model->get_active()
	  				));
	  $this->load->view('layouts/footer');
	}
	public function add_form(){
				$this->load->view($this->session->userdata('foradd'),array(
					'accounts'		=> $this->edit_reading_model->get_active()
					));
	}
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
			'reading_data' 			=> $this->edit_reading_model->get_read_data($id),
			'reading_id'			=> $id
		));
		
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
			'reading_data' 			=> $this->edit_reading_model->get_read_data($id)
		));
	}

	public function getprevious(){
		$date = $this->input->post('date');
		$date_due = date('d-M-y' , strtotime('+15 days',strtotime($date)));
		$result = $this->edit_reading_model->get_latest_read($this->input->post('acct_id'));
		///echo json_encode($result);
		if(date("mY", strtotime($result[0]->reading_date)) >= date("mY" , strtotime($this->input->post('date')))){
			echo json_encode("false");
		}else{
			echo json_encode(array('result' => $result , 'due_date' => $date_due , 'current' => date('d-M-y' , strtotime($date)) , 'now_month' => date('m', strtotime($date)) , 'now_year' => date('Y' ,strtotime($date))));
		}
	}

	public function getprevious2(){
		$stats;
		//$date = $this->input->post('date');
		$date = date('d-m-y');
		$rid = $this->input->post('reading_id');
		$aid = $this->input->post('acct_id');
		$date_due = date('d-M-y' , strtotime('+15 days',strtotime($date)));
		$result = $this->edit_reading_model->get_latest_read($this->input->post('acct_id'));
		$bill_bal = $this->edit_reading_model->get_bill_bal($this->input->post('acct_id'));
		$r2 = $this->edit_reading_model->get_max_readdate($aid);
		$pay = $this->edit_reading_model->get_pay($rid);
		//echo json_encode($r2);
		if($rid == $r2[0]->reading_id){
			$stats = TRUE;
		}else{
			$stats = FALSE;
		}
		//echo ('1:'.date("mY", strtotime($result[0]->reading_date)).' 2:'.date("mY"));
		echo json_encode(array('result' => $result ,'result2' => $bill_bal, 'due_date' => $date_due , 'current' => date('d-M-y' , strtotime($date)) , 'now_month' => date('m', strtotime($date)) , 'now_year' => date('Y' ,strtotime($date)),'status' => $stats));
	}

	public function geth(){
		$query  = $this->edit_reading_model->geth($this->input->post('acct_id'),$this->input->post('acct_id'),$this->input->post('acct_id'),$this->input->post('acct_id'));
		echo json_encode(array('r1' => $query));
	}

	public function getcharges_rates(){
		$query  = $this->edit_reading_model->acct_rates($this->input->post('acct_id'));
		$query1 = $this->edit_reading_model->acct_chrge($this->input->post('acct_id'));
		$query2 = $this->edit_reading_model->geth(array($this->input->post('acct_id'),$this->input->post('consumed')));
		echo json_encode(array('r1' => $query ,'r2'  => $query1, 'r3'  => $query2));
	}

	public function getcharges_rates1(){
		$query  = $this->edit_reading_model->acct_rates($this->input->post('acct_id'));
		$query1 = $this->edit_reading_model->acct_chrge1($this->input->post('reading_id'));
		$query2 = $this->edit_reading_model->geth(array($this->input->post('acct_id'),$this->input->post('consumed')));
		echo json_encode(array('r1' => $query ,'r2'  => $query1, 'r3' => $query2 ));
	}

	public function getcharges_rates_v(){
		$query  = $this->edit_reading_model->acct_rates($this->input->post('acct_id'));
		$query1 = $this->edit_reading_model->used_acct_chrge($this->input->post('acct_id'),$this->input->post('reading_id'));
		echo json_encode(array('r1' => $query ,'r2'  => $query1 ));
	}

	public function search_ubs_mtr(){

			$id			= $this->input->post('acct');
	 		$date		= $this->input->post('ubs_ym');
	 		$rid; $status;
	 		$query = $this->edit_reading_model->get_ubs_mtr($id,$date);

	 		foreach($query as $q){
	 			$rid = $q->reading_id;
	 		}
	 		$query1 = $this->edit_reading_model->ifpay($rid);
	 		if(!empty($query1)){
	 			$status = FALSE;
	 		}else{
	 			$status = TRUE;
	 		}
			if(!empty($query)) {
			echo json_encode(array('r1' => $query, 'r2' => $status));
			}else{
				echo json_encode("none");
			}		
	}

	public function save_reading(){
		$data3 = array(
			  $this->input->post('reading_id')
			 ,':p_meter_reading' => $this->input->post('reading')
			 ,NULL
			 ,NULL
			 ,NULL
			 ,NULL
			 ,NULL
			 ,$this->session->userdata('user_id')
			 ,NULL
			 ,"READINGONLY"
		);

		$result = $this->edit_reading_model->save_reading($data3);

	}

	public function save_read(){
		$query1 = $this->edit_reading_model->acct_chrge($this->input->post('id'));

		$data3 = array(
			  ':p_account_id'    => $this->input->post('id')
			 ,':p_prev_read'     => $this->input->post('prev_r')
			 ,':p_prev_date'     => $this->input->post('prev_d')
			 ,':p_meter_reading' => $this->input->post('c_read')
			 ,':p_reading_date'  => $this->input->post('c_date')
			 ,':p_consumed'      => $this->input->post('cons')
			 ,':p_bill_date'     => $this->input->post('b_date')
			 ,':p_bill_month'    => $this->input->post('b_month')
			 ,':p_bill_year'     => $this->input->post('b_year')
			 ,':p_read_amt'      => $this->input->post('r_amt')
			 ,':p_bill_bal'      => $this->input->post('bill_bal')
			 ,':p_total_due'     => $this->input->post('total_d')
			 ,':p_curchg'        => $this->input->post('c_amt')
			 ,':p_adv_pay'		 => ''
			 ,':p_due_date'      => $this->input->post('due_d')
			 ,':p_print_flag'    => $this->input->post('p_flag')
			 ,':p_print_by'      => $this->session->userdata('user_id')
			 ,':p_print_date'    => date('d-M-y')
			 ,':p_sp_flag'       => "N"
			 ,':p_sp_readType'   => ""
			 ,':p_sp_reason'     => ""
			 ,':p_max_rate'		 => $this->input->post('m_rate')
			 ,':p_remarks'		 => ""
			 ,':p_findings'		 => ""
		);
		//vardump($data3);
		$ym = $this->input->post('b_month').$this->input->post('b_year');
		$result = $this->edit_reading_model->save_reads($data3);
		if($result[0]->status == true){
			foreach ($query1 as $chg) {
				$data2 = array(
				':p_account_id' => $chg->acct_id,
				':p_chg_amt' 	=> $chg->chg_amount,
				':p_chg_code' 	=> $chg->charge_code,
				':p_chg_desc'	=> $chg->description,
				':p_yearMonth' 	=> $ym,
				':p_user_id' 	=> $this->session->userdata('user_id')
				);
				//var_dump($data2);
				$res2 = $this->edit_reading_model->insert_used_chg($data2);
				echo json_encode($result);
				// var_dump($data2);
			}
		}else{
			echo json_encode($result);
		}
		
	}

	public function edit_read(){
		$query1 = $this->edit_reading_model->acct_chrge($this->input->post('id'));

		$data3 = array(
			  $this->input->post('reading_id')
			 ,':p_meter_reading' => $this->input->post('c_read')
			 ,':p_consumed'      => $this->input->post('cons')
			 ,':p_read_amt'      => $this->input->post('r_amt')
			 ,':p_bill_bal'      => $this->input->post('bill_bal')
			 ,':p_total_due'     => $this->input->post('total_d')
			 ,':p_curchg'        => $this->input->post('c_amt')
			 ,':p_print_by'      => $this->session->userdata('user_id')
			 ,$this->input->post('m_rate')
			 ,""
		);
		$ym = $this->input->post('b_month').$this->input->post('b_year');
		$result = $this->edit_reading_model->edit_reads($data3);
		if($result[0]->status == true){
			foreach ($query1 as $chg) {
				$data2 = array(
				':p_account_id' => $chg->acct_id,
				':p_chg_amt' 	=> $chg->chg_amount,
				':p_chg_code' 	=> $chg->charge_code,
				':p_chg_desc'	=> $chg->description,
				':p_yearMonth' 	=> $ym,
				':p_user_id' 	=> $this->session->userdata('user_id')
				);
				//var_dump($data2);
				$res2 = $this->edit_reading_model->insert_used_chg($data2);
				echo json_encode($result);
				// var_dump($data2);
			}
		}else{
			echo json_encode($result);
		}
		
	}

	
	
	 
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */